package com.gengzp.mq.server.facade.impl;

import com.gengzp.mq.server.enums.MqMessageStatusEnums;
import com.gengzp.mq.server.facade.MqMessageFacadeService;
import com.gengzp.mq.server.mapper.MqMessageMapper;
import com.gengzp.mq.server.model.message.entity.MqMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName MqMessageFacadeServiceImpl
 * @Description 消息门面层实现类
 * @Author gengzp
 * @Date 2025/9/3 17:30
 */
@Service
public class MqMessageFacadeServiceImpl implements MqMessageFacadeService {

    @Autowired
    private MqMessageMapper mqMessageMapper;

    @Override
    public List<MqMessage> searchMqMessagesByIds(List<String> messageIds) {
        if (messageIds == null || messageIds.isEmpty()) {
            return Collections.emptyList();
        }
        return mqMessageMapper.selectMqMessagesByIds(messageIds);
    }

    @Override
    public List<MqMessage> searchConsumedSuccessMqMessagesByIds(List<String> messageIds) {
        if (messageIds == null || messageIds.isEmpty()) {
            return Collections.emptyList();
        }
        return mqMessageMapper.selectConsumedSuccessMqMessagesByIds(messageIds);
    }

    @Override
    public List<String> searchAllObsoleteMessageIds() {
        return mqMessageMapper.selectAllObsoleteMessageIds(System.currentTimeMillis());
    }

    @Override
    public List<MqMessage> searchMqMessagesByStatus(Integer status) {
        if (status == null) {
            return Collections.emptyList();
        }
        return mqMessageMapper.selectMessagesByStatus(status);
    }

    @Override
    public void batchEditMessageToDeath(List<String> messageIds) {
        if (messageIds == null || messageIds.isEmpty()) {
            return;
        }
        mqMessageMapper.batchUpdateMessageStatusByIds(messageIds, MqMessageStatusEnums.CONSUME_FAILED.getCode());
    }

    @Override
    public void editMessageById(MqMessage mqMessage) {
        if (mqMessage == null) {
            return;
        }
        mqMessageMapper.updateMessageById(mqMessage);
    }

    @Override
    public void removeMessagesByIds(List<String> messageIds) {
        if (messageIds == null || messageIds.isEmpty()) {
            return;
        }
        mqMessageMapper.deleteMessageByIds(messageIds);
    }

}
